#!/usr/bin/python3
# -*- coding: UTF-8 -*-

from app import App
from utils.tools import Tools
from utils.element import Element
import re as osRe

import gol
gol._init()
from os import popen as osPopen, path as osPath  # 管道处理

class Application(App):
    # 初始配置
    def __init__(self):
        super().__init__()

    # ////////-----------------

    def popupClose(self):
        with self._d.watch_context() as ctx:
            ctx.when("打开签到提醒").when('//*[@resource-id="com.ss.android.article.lite:id/ath"]/android.widget.FrameLayout[1]/com.lynx.tasm.behavior.ui.LynxFlattenUI[116]').click()
            ctx.when("领取成功").call(lambda d: d.press("back"))
            ctx.when("坚持退出").click()

    # 取秒数
    def getNum(self):
        valStr = self._d(textContains='s后可领奖励').get_text()
        num = int(osRe.findall(r'\d+', valStr)[0])
        print(num)
        return num + 6

    def toNext(self):
        self.tool.setSleep(2)
        try:
            num = self.getNum()
            print(num)
            self.tool.countDown(num)
            self._d.press("back")
            self.popupClose()
        except:
            self.popupClose()


    # 任务中心
    def renwu(self):
        print('任务')
        if (self.el2CE('t', '任务')):
            if (self.el2CE('t', '翻倍领取')):
                print('任务', ' - ', '弹窗 - 翻倍领取')
                self.toNext()
            if (self.el2CE('t', '开宝箱得金币')):
                print('任务', ' - ', '开宝箱得金币')
                if (self.el2CE('t', '看视频领')):
                    print('任务', ' - ', '弹窗 - 看视频领领xxx金币')
                    self.toNext()


    
        
    # 启动APP
    def startApp(self):
        sApp = 'com.ss.android.article.lite/com.ss.android.article.lite.activity.SplashActivity'
        self.tool.callApp(sApp)

    def killApp(self):
        self._d.app_stop_all()
        self.tool.setSleep(3)


class Action(Application):
    # 初始配置
    def __init__(self):
        super().__init__()

    def init(self):
        sAction = self.sAction
        if (sAction=='all'):
            self.renwu()
        elif (sAction=='start'):
            try:
                self.startApp()
                print("%s - 成功" %('APP启动'))
            except:
                print("%s - 失败" %('APP启动'))
        else:
            print("test")
            self.toNext()



action = Action()
action.init()
